'use strict';


const unipay = require('uni-pay');

const fs = require('fs');

const path = require('path')


const unipayIns = unipay.initWeixin({

  appId: 'wxab056e746467b13f',//利用在对应领取平台的appId

  mchId: '1616845953',//商户号

  key: 'QWERTYUOPASDFGHJKLZXCVBNM1234567',//领取秘钥
  // 退款必须
  pfx: fs.readFileSync(path.resolve(__dirname, 'cert/apiclient_cert.p12')) 

})

exports.main = async (event, context) => {
	//event为客户端上传的参数
	console.log('OK')
	console.log(event)
	if (event.action === 'return') {
		let orderInfo = await unipayIns.refund({
			outTradeNo: event.order_number,
			outRefundNo: event.outRefundNo, // 支付宝可不填此项
			totalFee: event.money * 100, // 订单总金额，支付宝可不填此项
			refundFee: event.money * 100 // 退款总金额
		})	
		return {
		    orderInfo
		}
	}
	if (event.action === 'select') {
		let orderInfo = await unipayIns.refundQuery({
			outTradeNo: event.order_number
		})	
		return {
		    orderInfo
		}
	}
};
